/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.process;

import com.sun.management.OperatingSystemMXBean;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import rie.model.CPU;
import rie.model.Memoire;
import rie.model.Processus;
import rie.model.SystemeInfo;

/**
 *
 * @author Michael
 */
public class DisplayProcess {
    
    
  public static List<Processus> listRunningProcessesWindows() {
        List<Processus> tasklist = new ArrayList();
        try {
            String []command = new String[] {"tasklist","/fo","csv","/nh"};
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader input = new BufferedReader
            (new InputStreamReader(process.getInputStream()));
            String line ="";
            while ((line = input.readLine()) != null) {
            if(!line.equals("")) {
                line = line.replaceAll("\"","");
                Processus processus = new Processus();
                String[] taskAttributes = null;
                try {
                    taskAttributes = line.split(",");
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
                    processus.setName(taskAttributes[0]==null?"":taskAttributes[0]);
                    processus.setPid(taskAttributes[1]==null?"":taskAttributes[1]);
                try {
                    processus.setMemoryUsage(taskAttributes[4]==null?"":taskAttributes[4]+taskAttributes[5]==null?"":taskAttributes[5]);
                } catch (RuntimeException e) {
                    processus.setMemoryUsage(taskAttributes[4]==null?"":taskAttributes[4]);
                }
                tasklist.add(processus);
            }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tasklist;
        
   }
   public static List<Processus> listRunningProcessesMac() {  
       return null;
   }
   public static long getFreeMemoryWin(){
       long memoire;
       OperatingSystemMXBean os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
       memoire = os.getFreePhysicalMemorySize() / (1024*1024);       
       return memoire;
   }
   
   public static int getCPUWin(){
       int cpu =0;
       String []command = new String[] {"wmic","cpu","get","loadpercentage"};
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line ="";
            line = input.readLine();
            line = input.readLine();
            line = input.readLine().trim();
            cpu = Integer.valueOf(line).intValue();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cpu;
   }
   public static SystemeInfo getSystemeInfo(){
       SystemeInfo retour=null;
       if(System.getProperty("os.name").contains("Windows")){
           retour = new SystemeInfo(new CPU(getCPUWin()),new Memoire(getFreeMemoryWin()),listRunningProcessesWindows());
        }else if(System.getProperty("os.name").contains("OSX")){
            //processes = listRunningProcessesMac();
        }
       return retour;
   }
   
  
    public static void main(String args[]) {
        List<Processus> processes = null;
        int cpu = 0;
        long mem = 0;
        if(System.getProperty("os.name").contains("Windows")){
            processes = listRunningProcessesWindows();
            cpu = getCPUWin();
             mem = getFreeMemoryWin();
        }else if(System.getProperty("os.name").contains("OSX")){
            processes = listRunningProcessesMac();
        }
        
        String result = "";

        Iterator<Processus> it = processes.iterator();
        int i = 0;
        while (it.hasNext()) {
           Processus p = it.next();
           result += p.getName()+"|"+p.getPid()+",";
           i++;
           if (i==10) {
               result += "\n";
               i = 0;
           }
        }
        System.out.println(result);
        System.out.println("CPU Usage: "+cpu+"%.");
        System.out.println("Free mem: "+mem +" Mo.");
    }    
}
